
#include "ItemModifyDlg.h"

ItemModifyDlg::ItemModifyDlg(WorkerModel::WebdavCfg cfg, QWidget *parent)
    : QDialog(parent)
    , m_cfg(cfg)
{
    setWindowModality(Qt::WindowModality::ApplicationModal);
    resize(600, 360);

    QVBoxLayout *contentLayout = new QVBoxLayout(this);
    contentLayout->setContentsMargins(5, 5, 5, 5);

    QLabel *connTypeTitleLabel = new QLabel(this);
    connTypeTitleLabel->setText("连接类型：");
    contentLayout->addWidget(connTypeTitleLabel, 0, Qt::AlignLeft);

    QComboBox *connTypeBox = new QComboBox(this);

    connTypeBox->addItems({"http", "https"});
    if (m_cfg.ConnType == QWebdav::HTTP) {
        connTypeBox->setCurrentIndex(0);
    } else if (m_cfg.ConnType == QWebdav::HTTPS) {
        connTypeBox->setCurrentIndex(1);
    }
    contentLayout->addWidget(connTypeBox);

    QLabel *hostNameTitleLabel = new QLabel(this);
    hostNameTitleLabel->setText("主机名：");
    contentLayout->addWidget(hostNameTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *hostNameLineEdit = new QLineEdit(this);
    hostNameLineEdit->setText(m_cfg.HostName);
    contentLayout->addWidget(hostNameLineEdit);

    QLabel *rootPathTitleLabel = new QLabel(this);
    rootPathTitleLabel->setText("根路径：");
    contentLayout->addWidget(rootPathTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *rootPathLineEdit = new QLineEdit(this);
    rootPathLineEdit->setText(m_cfg.RootPath);
    contentLayout->addWidget(rootPathLineEdit);

    QLabel *userNameTitleLabel = new QLabel(this);
    userNameTitleLabel->setText("用户名：");
    contentLayout->addWidget(userNameTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *userNameLineEdit = new QLineEdit(this);
    userNameLineEdit->setText(m_cfg.UserName);
    contentLayout->addWidget(userNameLineEdit);

    QLabel *passwordTitleLabel = new QLabel(this);
    passwordTitleLabel->setText("密码：");
    contentLayout->addWidget(passwordTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *passwordLineEdit = new QLineEdit(this);
    passwordLineEdit->setText(m_cfg.Password);
    contentLayout->addWidget(passwordLineEdit);

    QLabel *portTitleLabel = new QLabel(this);
    portTitleLabel->setText("端口：");
    contentLayout->addWidget(portTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *portLineEdit = new QLineEdit(this);
    portLineEdit->setText(QString::number(m_cfg.Port));
    contentLayout->addWidget(portLineEdit);

    QLabel *localPathTitleLabel = new QLabel(this);
    localPathTitleLabel->setText("本地路径：");
    contentLayout->addWidget(localPathTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *localPathLineEdit = new QLineEdit(this);
    localPathLineEdit->setText(m_cfg.LocalDirPath);
    contentLayout->addWidget(localPathLineEdit);

    QLabel *remotePathTitleLabel = new QLabel(this);
    remotePathTitleLabel->setText("远程路径：");
    contentLayout->addWidget(remotePathTitleLabel, 0, Qt::AlignLeft);

    QLineEdit *remotePathLineEdit = new QLineEdit(this);
    remotePathLineEdit->setText(m_cfg.RemoteDirPath);
    contentLayout->addWidget(remotePathLineEdit);

    contentLayout->addStretch(1);
    QPushButton *okBtn = new QPushButton(this);
    okBtn->setText("确定");
    contentLayout->addWidget(okBtn, 0, Qt::AlignCenter);

    // connection
    connect(okBtn, &QPushButton::clicked, this, [=] {
        if (connTypeBox->currentIndex() == 0) {
            m_cfg.ConnType = QWebdav::QWebdavConnectionType::HTTP;
        } else if (connTypeBox->currentIndex() == 1) {
            m_cfg.ConnType = QWebdav::QWebdavConnectionType::HTTPS;
        }
        m_cfg.HostName = hostNameLineEdit->text();
        m_cfg.RootPath = rootPathLineEdit->text();
        m_cfg.UserName = userNameLineEdit->text();
        m_cfg.Password = passwordLineEdit->text();
        m_cfg.Port = portLineEdit->text().toInt();
        m_cfg.LocalDirPath = localPathLineEdit->text();
        m_cfg.RemoteDirPath = remotePathLineEdit->text();
        done(1);
    });
}

ItemModifyDlg::~ItemModifyDlg()
{
}

WorkerModel::WebdavCfg ItemModifyDlg::GetCfg()
{
    return m_cfg;
}

